package com.nutiteq.mapper;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.nutiteq.components.ZoomRange;
import com.nutiteq.ui.ZoomIndicator;
import com.nutiteq.utils.Utils;

public class CustomZoomIndicator implements ZoomIndicator {
  private final Image zoomInImage;
  private final Image zoomOutImage;
  private boolean visible;
  private ZoomRange zoomRange;
  private final Font zoomFont = Font.getDefaultFont();
  private final int boxWidth;
  private final int boxHeght;
  private int previousZoom = -1;
  private boolean zoomedIn;

  public CustomZoomIndicator() {
    zoomInImage = Utils.createImage("/zoom_in.png");
    zoomOutImage = Utils.createImage("/zoom_out.png");

    boxWidth = zoomInImage.getWidth() + zoomFont.stringWidth("66");
    boxHeght = Math.max(zoomFont.getHeight(), zoomInImage.getHeight());
  }

  public long displayTime() {
    return 3000;
  }

  public boolean isVisible() {
    return visible;
  }

  public void paint(final Graphics g, final int currentZoom, final int displayWidth,
      final int displayHeight) {
    if (currentZoom != previousZoom) {
      zoomedIn = currentZoom > previousZoom;
      previousZoom = currentZoom;
    }

    g.setClip(0, 0, displayWidth, displayHeight);
    g.setColor(0xFFFFFFFF);
    g.fillRect(displayWidth - boxWidth, 0, boxWidth, boxHeght);
    g.drawImage(zoomedIn ? zoomInImage : zoomOutImage, displayWidth - boxWidth, 0, Graphics.TOP
        | Graphics.LEFT);
    g.setColor(0xFF000000);
    g.setFont(zoomFont);
    g.drawString(Integer.toString(currentZoom), displayWidth - boxWidth + zoomInImage.getWidth(),
        0, Graphics.TOP | Graphics.LEFT);
  }

  public void setVisible(final boolean visible) {
    this.visible = visible;
  }

  public void setZoomRange(final ZoomRange zoomRange) {
    this.zoomRange = zoomRange;
  }

  public void setCurrentZoom(final int zoom) {
    previousZoom = zoom;
  }
}
